package com.innovation.chengzhangriji.ui;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.widget.PhotosAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimelinePhotoActivity extends BaseTimelinePhotoAndCameraActivity implements PhotosAdapter.OnClickAdapterListener {

    private PhotosAdapter mPhotoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startTask();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_timeline_photo);
        RecyclerView photosView = (RecyclerView) findViewById(R.id.photos_view);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 4);
        photosView.setLayoutManager(layoutManager);
        mPhotoAdapter = new PhotosAdapter(this);
        mPhotoAdapter.setOnClickAdapterListener(this);
        photosView.setAdapter(mPhotoAdapter);
    }

    @Override
    protected String[] getTimelineUrls() {
        return mPhotoAdapter.getUrls();
    }

    @Override
    protected void onPageStart() {
        MobclickAgent.onPageStart(Config.UMENG_PAGE_TIMELINE_PHOTO);
    }

    @Override
    protected void onPageEnd() {
        MobclickAgent.onPageEnd(Config.UMENG_PAGE_TIMELINE_PHOTO);
    }

    @Override
    public void onClickRight() {
        if (mPhotoAdapter.getUrls().length == 0) {
            tipShow("请选择照片");
            return;
        }
        super.onClickRight();
    }

    private void startTask() {
        LoadImageTask a = new LoadImageTask();
        a.execute();
    }

    private void queryAllImage() {
        String[] projection = new String[]{MediaStore.Images.Media.DATA};
        Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        Cursor cursor = getContentResolver().query(images,
                projection,
                null,
                null,
                MediaStore.Images.Media.DATE_ADDED + " DESC"
        );
        if (cursor.moveToFirst()) {
            String data;
            int dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            do {
                data = cursor.getString(dataColumn);
                /*arrayList.add(data);*/
                mPhotoAdapter.addImageUrl(data);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

    @Override
    public void onClickAdd() {
        addPhoto();
    }

    @Override
    public void onClickRemove() {
        removePhoto();
    }

    private class LoadImageTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            queryAllImage();
            return null;
        }

    }
}
